import { ref } from "vue"

export const useInputData = (op) => {
  const { APIS, plugSubmit, editDone, addDone } = op

  const form = ref({})
  const onSubmit = (data) => {

    
    if (plugSubmit) {
      data = plugSubmit(data) || data
    }
    const plugForm = form.value || {}
    if (data.id) {
      APIS.update({ ...data, ...plugForm }).then((res) => {
        uni.showToast({
          title: "修改成功",
          icon: "succes",
        })
        editDone && editDone()
      })
    } else {
      APIS.add({ ...data, ...plugForm }).then((res) => {
        uni.showToast({
          title: "新增成功",
          icon: "succes",
        })
        addDone && addDone()
      })
    }
  }

  return {
    onSubmit,
    form
  }
}
